package mysql

import (
	"shuqi-novel/novelrpc/novelrpc"

	"gorm.io/gorm"
)

type Novel struct {
	gorm.Model
	Name      string `gorm:"type:varchar(30);not null"`
	Title     string `gorm:"type:varchar(50);not null"`
	Content   string `gorm:"type:varchar(200);not null"`
	Author    string `gorm:"type:varchar(30);not null"`
	NovelType string `gorm:"type:varchar(50);not null"`
	UserId    int64  `gorm:"type:int(11);not null"`
}

func (n *Novel) NovelAdd(db *gorm.DB, UserId int64) error {
	return db.Debug().Where("userId=?", UserId).Create(n).Error
}

func (n *Novel) NovelUpdate(db *gorm.DB, id uint) error {
	return db.Debug().Where("id=?", id).Updates(n).Error
}

func NovelList(db *gorm.DB, in *novelrpc.NovelListRequest) ([]*novelrpc.NovelList, error) {
	var list []*novelrpc.NovelList
	err2 := db.Debug().Model(&Novel{}).Scopes(Paginate(int(in.Page), int(in.Size))).Find(&list).Error
	return list, err2
}
